package com.exam.scjp5.objectives.section_3;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExampleRegex
{
    public static void main(String args[])
    {
        Pattern pattern = Pattern.compile(".*xx");
        Matcher matcher = pattern.matcher("yyyxxzzzxxxxxx");
        System.out.println(".*xx => ");
        while(matcher.find())
            System.out.println( matcher.start() + " : " + matcher.group());
        
        pattern = Pattern.compile(".*?xx");
        matcher = pattern.matcher("yyyxxzzzxxxxxx");
        System.out.println("\n.*?xx => ");
        while(matcher.find())
            System.out.println(matcher.start() + " : " + matcher.group());
        
        pattern = Pattern.compile(".+?xx");
        matcher = pattern.matcher("yyyxxzzzxxxxxx");
        System.out.println("\n.+?xx => ");
        while(matcher.find())
            System.out.println(matcher.start() + " : " + matcher.group());
        
        pattern = Pattern.compile("\\d*");
        matcher = pattern.matcher("ab234d5ef6");
        System.out.println("\n\\d* => ");
        while(matcher.find())
            System.out.println(matcher.start() + " : " + matcher.group());
        
        pattern = Pattern.compile(".*xzz");
        matcher = pattern.matcher("yyyxxzzzxxxxxx");
        System.out.println("\n.*zz => ");
        while(matcher.find())
            System.out.println(matcher.start() + " : " + matcher.group());
        
        pattern = Pattern.compile("\\w+(\\.?\\w+)*@\\w+\\.\\w+(\\.\\w+)*");
        matcher = pattern.matcher(
                "a@a.com,  ab@b.com.sg,  c@c.com.co.uk,  " +
                "a_._0a.a@c.com.sg, hello.hello@@bcd.com.sg");
        System.out.println("\n\\w+(\\.?\\w+)*@\\w+\\.\\w+(\\.\\w+)* EMAIL  => ");
        while(matcher.find())
            System.out.println(matcher.start() + " : " + matcher.group());
        
        matcher = pattern.matcher("Hello");
        if(matcher.find())
            System.out.println(matcher.start() + ":" + matcher.group());
        else
            System.out.println("Invalid Email " + matcher.start() + ":" + matcher.group());
        
    }
}
